﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rud.Holmgren.TabulexLib
{
    public static class TbxUtil
    {
        public static long ToTbxTime(DateTime dt)
        {
            var epoc = new DateTime(1970, 1, 1);
            var txts = (long)((DateTime.Now.ToUniversalTime() - epoc).TotalMilliseconds / 1000.0);
            return txts;
        }

        public static DateTime FromTbxTime(long txts)
        {
            var epoc = new DateTime(1970, 1, 1);
            long tick = 1000L * txts;
            var dt = epoc.AddMilliseconds(tick).ToLocalTime();
            return dt;
        }

        public static string getRandomId(int seed)
        {
            var rnd = new Random(seed);
            var bytes = new byte[64 / 8];
            rnd.NextBytes(bytes);

            return String.Join("", bytes.Select(b => String.Format("{0:x02}", b)));
        }

        public static String getLogIdentifier()
        {
            String str1 = "";
            try
            {
                String str2 = getRandomId(90210); // Settings.Secure.getString(paramContext.getContentResolver(), "android_id"); "c303e7e3195f9af6";
                int i = 21; // getVersionCode(paramContext);
                String str3 = "1.5.4"; // Build.VERSION.RELEASE;
                String str4 = "ST27i"; //  Build.MODEL;
                String str5 = "ST27i_1265-2685"; //  Build.PRODUCT;
                str1 = str3 + ";" + i + ";" + str4 + " " + str5 + ";" + str2;
                // Log.d("logident", str1);
                return str1;
            }
            catch (Exception)
            {
                // Log.e("Could not make log identifier");
            }
            return str1;
        }

#if false
        public static T GetObject<T>(this IDictionary<string, object> dict, string name, T fallback = default(T)) 
        {
            return dict.ContainsKey(name) ? (T) dict[name] : fallback;
        }

        public static IDictionary<string, object> GetChildJson(this IDictionary<string, object> dict, string name)
        {
            return dict.GetObject<IDictionary<string, object>>(name);
        }

        public static string GetString(this IDictionary<string, object> dict, string name, string fallback = null)
        {
            return dict.GetObject<string>(name, fallback);
        }

        public static int GetInt(this IDictionary<string, object> dict, string name, int fallback = 0)
        {
            return (int) dict.GetObject<long>(name, fallback);
        }

        public static IEnumerable<IDictionary<string, object>> GetObjectArray(this IDictionary<string, object> dict, string name)
        {
            var list = dict.GetObject<List<object>>(name);
            return list != null ? list.Cast<IDictionary<string, object>>().ToList() : new List<IDictionary<string, object>>();
        }
#endif
    }

}
